package com.lc.qianrenshiye.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lc.qianrenshiye.BaseActivity;
import com.lc.qianrenshiye.BaseApplication;
import com.lc.qianrenshiye.R;
import com.lc.qianrenshiye.adapter.MyCastAdapter;
import com.lc.qianrenshiye.conn.MycastListPost;
import com.lc.qianrenshiye.dialog.DateTimePickDialogUtil;
import com.lc.qianrenshiye.uitls.MyAnimationUitls;
import com.lc.qianrenshiye.uitls.MyToast;
import com.lc.qianrenshiye.uitls.TimeUtil;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/11/21.
 */
public class MyCastActivity extends BaseActivity implements View.OnClickListener {

    //最外层布局

    @BoundView(R.id.mycast_lv) private PullToRefreshListView pullToRefreshListView;
    private ListView mycast_lv;

    @BoundView(R.id.mycast_tab_rg) private RadioGroup mycast_tab_rg;
    @BoundView(R.id.mycast_tab_order_rg) private RadioGroup mycast_tab_order_rg;
    @BoundView(R.id.mycast_tab_immediate_rb) private RadioButton mycast_tab_immediate_rb;
    @BoundView(R.id.mycast_tab_order_date_rb) private RadioButton mycast_tab_order_date_rb;
    @BoundView(R.id.mycast_time_frame_tv) private TextView mycast_time_frame_tv;
    @BoundView(R.id.mycast_total_tv) private TextView mycast_total_tv;
    //时间搜索的东西
    @BoundView(R.id.mycast_search_tv) private TextView mycast_search_tv;
    @BoundView(R.id.mycast_time_end_tv) private TextView mycast_time_end_tv;
    @BoundView(R.id.mycast_time_start_tv) private TextView mycast_time_start_tv;
    @BoundView(R.id.mycast_search_time_ll) private LinearLayout mycast_search_time_ll;

    //筛选那一行
    @BoundView(R.id.mycast_tab_detault_tv) private TextView mycast_tab_detault_tv;
    @BoundView(R.id.mycast_tab_list_time_tv) private TextView mycast_tab_list_time_tv;
    @BoundView(R.id.mycast_tab_order_tv) private TextView mycast_tab_order_tv;

    private List<MycastListPost.MycastEntity> lists = new ArrayList<>();

    private MyCastAdapter adapter;

    private boolean isPullRefresh; //控制 radiobutton 下拉刷新时候改变状态 不让走接口

    private MycastListPost mycastListPost = new MycastListPost(new AsyCallBack<MycastListPost.MycastListEntity>(){

        @Override
        public void onSuccess(String toast, int type, MycastListPost.MycastListEntity mycastListEntity) throws Exception {

            //其实都清空，就加载时候不清空

            if (type == 0) {

                lists.clear();

            } else if (type == 1) {

                lists.clear();

                MyAnimationUitls.toVisibleAnim(mycast_lv);// 就是一个显示的动画

            } else if (type == 2) {

                lists.clear();

                isPullRefresh = true;

                mycast_time_start_tv.setText("");
                mycast_time_end_tv.setText("");

                mycast_tab_immediate_rb.setChecked(true);
                mycast_tab_order_date_rb.setChecked(true);

            }

            mycast_total_tv.setText(mycastListEntity.total);

            mycast_time_frame_tv.setText(mycastListEntity.start + "—" + mycastListEntity.end);

            lists.addAll(mycastListEntity.mycastEntityList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            MyToast.showToast(toast, context);
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullUpRefreshComplete();
            pullToRefreshListView.onPullDownRefreshComplete();

            setLastUpdateTime();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycast);

        setBackTrue();

        setTitleName(getString(R.string.center_my_cast_text));

        findViewById(R.id.title_bottom_line).setVisibility(View.GONE);

        View view = BoundViewHelper.boundView(this, ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) LayoutInflater.from(this).inflate(R.layout.activity_mycast_head, null)));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        mycast_time_start_tv.setHint(simpleDateFormat.format(new Date()).toString());
        mycast_time_end_tv.setHint(simpleDateFormat.format(new Date()).toString());

        pullToRefreshListView.setPullRefreshEnabled(true);
        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {


                mycastListPost.sid = BaseApplication.BasePreferences.readShopId();
                mycastListPost.time = "day";
                mycastListPost.type = "time";
                mycastListPost.start = "";
                mycastListPost.end = "";

                mycastListPost.execute(MyCastActivity.this, false, 2);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

//                mycastListPost.execute(MyCastActivity.this, false);
                pullToRefreshListView.onPullUpRefreshComplete();

            }
        });

        mycast_lv = pullToRefreshListView.getRefreshableView();

        mycast_lv.setVisibility(View.GONE);

        mycast_lv.addHeaderView(view, null, false);

        mycast_lv.setDivider(null);

        mycast_lv.setVerticalScrollBarEnabled(false);

        mycast_lv.setAdapter(adapter = new MyCastAdapter(context, lists));

        mycast_tab_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {

                    case R.id.mycast_tab_immediate_rb:

                        mycast_tab_list_time_tv.setText("列表数据：即时");

                        if (isPullRefresh) {

                            return;
                        }

                        mycastListPost.start = "";
                        mycastListPost.end = "";
                        mycastListPost.time = "day";
                        mycastListPost.execute(MyCastActivity.this);

                        break;

                    case R.id.mycast_tab_day_rb:

                        mycast_tab_list_time_tv.setText("列表数据：一天");

                        mycastListPost.start = "";
                        mycastListPost.end = "";
                        mycastListPost.time = "day";
                        mycastListPost.execute(MyCastActivity.this);

                        break;

                    case R.id.mycast_tab_month_rb:

                        mycast_tab_list_time_tv.setText("列表数据：月份");

                        mycastListPost.start = "";
                        mycastListPost.end = "";
                        mycastListPost.time = "month";
                        mycastListPost.execute(MyCastActivity.this);

                        break;

                    case R.id.mycast_tab_year_rb:

                        mycast_tab_list_time_tv.setText("列表数据：年份");

                        mycastListPost.start = "";
                        mycastListPost.end = "";
                        mycastListPost.time = "year";
                        mycastListPost.execute(MyCastActivity.this);

                        break;

                }
            }
        });

        mycast_tab_order_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {

                    case R.id.mycast_tab_order_date_rb:

                        mycast_tab_order_tv.setText("排序：按日期");

                        if (isPullRefresh) {

                            isPullRefresh = false;

                            return;
                        }

                        mycastListPost.type = "time";
                        mycastListPost.execute(MyCastActivity.this);
                        break;

                    case R.id.mycast_tab_order_total_price_rb:

                        mycast_tab_order_tv.setText("排序：按总价");

                        mycastListPost.type = "total";
                        mycastListPost.execute(MyCastActivity.this);
                        break;
                }
            }
        });

        mycastListPost.sid = BaseApplication.BasePreferences.readShopId();
        mycastListPost.time = "day";
        mycastListPost.type = "time";
        mycastListPost.start = "";
        mycastListPost.end = "";
        mycastListPost.execute(this, 1);


        mycast_tab_detault_tv.setOnClickListener(this);
        mycast_tab_list_time_tv.setOnClickListener(this);
        mycast_tab_order_tv.setOnClickListener(this);
        mycast_search_tv.setOnClickListener(this);
        mycast_time_end_tv.setOnClickListener(this);
        mycast_time_start_tv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        DateTimePickDialogUtil dateUtil;

        switch (v.getId()) {

            case R.id.mycast_tab_detault_tv:

                mycast_tab_detault_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_choose_bg_color));
                mycast_tab_list_time_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_bg_color));
                mycast_tab_order_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_bg_color));

                mycast_search_time_ll.setVisibility(View.VISIBLE);
                mycast_tab_rg.setVisibility(View.GONE);
                mycast_tab_order_rg.setVisibility(View.GONE);

                break;

            case R.id.mycast_tab_list_time_tv:

                mycast_tab_detault_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_bg_color));
                mycast_tab_list_time_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_choose_bg_color));
                mycast_tab_order_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_bg_color));

                mycast_search_time_ll.setVisibility(View.GONE);
                mycast_tab_order_rg.setVisibility(View.GONE);
                mycast_tab_rg.setVisibility(View.VISIBLE);

                break;

            case R.id.mycast_tab_order_tv:

                mycast_tab_detault_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_bg_color));
                mycast_tab_list_time_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_bg_color));
                mycast_tab_order_tv.setBackgroundColor(getResources().getColor(R.color.income_tab_choose_bg_color));

                mycast_search_time_ll.setVisibility(View.GONE);
                mycast_tab_rg.setVisibility(View.GONE);
                mycast_tab_order_rg.setVisibility(View.VISIBLE);

                break;

            case R.id.mycast_time_start_tv:

                dateUtil = new DateTimePickDialogUtil(MyCastActivity.this, null);

                dateUtil.dateTimePicKDialog(mycast_time_start_tv);

                break;

            case R.id.mycast_time_end_tv:

                dateUtil = new DateTimePickDialogUtil(MyCastActivity.this, null);

                dateUtil.dateTimePicKDialog(mycast_time_end_tv);

                break;

            case R.id.mycast_search_tv:

                String startTime = mycast_time_start_tv.getText().toString();
                String entTime = mycast_time_end_tv.getText().toString();

                if (TextUtils.isEmpty(startTime)) {

                    MyToast.showToast("请选择开始时间", context);
                    return;

                } else if (TextUtils.isEmpty(entTime)){

                    MyToast.showToast("请选择结束时间", context);
                    return;

                }

                if (Long.valueOf(TimeUtil.dataOne(entTime)) - Long.valueOf(TimeUtil.dataOne(startTime)) < 0) {

                    MyToast.showToast("起始时间 大于 结束时间", context);
                    return;
                }

                mycastListPost.sid = BaseApplication.BasePreferences.readShopId();
                mycastListPost.time = "";
                mycastListPost.type = "";
                mycastListPost.start = startTime;
                mycastListPost.end = entTime;
                mycastListPost.execute(this);

                break;

        }

    }

    private void setLastUpdateTime() {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String time = sDateFormat.format(System.currentTimeMillis());

        pullToRefreshListView.setLastUpdatedLabel(time);

    }


}
